﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TreeView;

namespace AlarmClock.BLL
{
    public class MyImageAnimator 
    {
        private static Bitmap _bitmap = null;
        private static Control _ctrl = null;

        public static void InitImageAnimator(Control ctrl, string gifPath)
        {
            Dispose();
            _ctrl = ctrl;
            _bitmap = new Bitmap(gifPath);
            if (_bitmap != null)
            {
                // 绘制GIF
                switch (ctrl)
                {
                    case Form _form:
                        _form.Paint += (s, e) =>
                        {
                            int x = (_form.Size.Width - 300) / 2 + (_form.Size.Width - 300) % 2;
                            int y = (_form.Size.Height - x - 300) / 2 + (_form.Size.Height - x - 300) % 2;
                            e.Graphics.DrawImage(_bitmap, x, y, 300, 300);     //从指定的位置绘制图片
                            ImageAnimator.UpdateFrames();                   //使该帧在当前正被图画处理的所有图像中前移
                        };
                        break;
                    default:
                        _ctrl.Paint += (s, e) =>
                        {
                            e.Graphics.DrawImage(_bitmap, 0, 0, ctrl.Width, ctrl.Height);     //从指定的位置绘制图片
                            ImageAnimator.UpdateFrames();                   //使该帧在当前正被图画处理的所有图像中前移
                        };
                        break;
                }
            }
        }

        public static void Dispose()
        {
            _bitmap?.Dispose();
        }

        public static void Pause()
        {
            throw new NotImplementedException("待开发");
        }

        public static void Play()
        {
            throw new NotImplementedException("待开发");
        }

        public static void PlayRepeat()
        {
            //使控件的整个图片无效并导致重绘事件
            ImageAnimator.Animate(_bitmap, new EventHandler((s, e) => { _ctrl.Invalidate(); }));   //将多帧图片显示为动画图像
        }

        public static void RePlay()
        {
            //使控件的整个图片无效并导致重绘事件
            ImageAnimator.Animate(_bitmap, new EventHandler((s, e) => { _ctrl.Invalidate(); }));   //将多帧图片显示为动画图像
        }

        public static void Stop()
        {
            // 停止动画
            ImageAnimator.StopAnimate(_bitmap, new EventHandler((s, e) => { _ctrl.Invalidate(); }));
        }

    }
}
